package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private CheckItemDao checkItemDao;
    @Override
    public Result add(CheckGroup checkGroup, Integer[] integers) {
        try {
            checkGroupDao.addGroup(checkGroup);
            Integer id = checkGroup.getId();
            for (Integer gid : integers) {
                checkGroupDao.addGroupAndItem(id,gid);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @Override
    public PageResult selectByPage(QueryPageBean queryPageBean) {
        int cunnrentPage = queryPageBean.getCurrentPage();
        int pageSize = queryPageBean.getPageSize();
        Page checkGroups=null;
        try {
            PageHelper.startPage(cunnrentPage,pageSize);
            checkGroups = checkGroupDao.selectAllByName(queryPageBean.getQueryString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new PageResult(checkGroups.getTotal(),checkGroups.getResult());
    }

    @Override
    public Integer[] findById(int id) {
        Integer[] integers=null;
        try {
            integers =  checkGroupDao.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return integers;
    }

    @Override
    public Result update(CheckGroup checkGroup,Integer[] checkitemIds) {
        try {
            checkGroupDao.update(checkGroup);
            if (checkitemIds!=null&&checkitemIds.length>0){
                checkGroupDao.deleteById(checkGroup.getId());
            }
            for (Integer checkitemId : checkitemIds) {
                checkGroupDao.addGroupAndItem(checkGroup.getId(),checkitemId);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    @Override
    public Result deleteOne(int id) {
        try {
            int ICount = checkGroupDao.selectCountById(id);
            int SCount = checkGroupDao.findCountById(id);
            if (ICount>0||SCount>0){
                return new Result(false,"您没有权限删除哦");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        checkGroupDao.deleteByGId(id);
        return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    @Override
    public List<CheckGroup> findAll() {
        List<CheckGroup> checkGroupList = null;
        try {
            checkGroupList = checkGroupDao.findAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return checkGroupList;
    }
}
